////////////////////////////////////////////////////////////////////////////////
// Auteur	: Maxime Vanier et Patrick Losier
// date		: 04/03/2013
//
// description : Execution du programme de tri
///////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>

#include <string>
#include <map>
#include <vector>

#include "Executor.h"

void extractConfig(std::string& file, std::multimap<int, std::string>& map, int key);

int main( int argc, const char* argv[] )
{
	printf("START\n");

	bool vorace = false;
	bool dynamique = false;
	bool BaB = false;
	std::multimap<int, std::string> echantillons;
	int mapIndex = 0;

	for (int i = 1 ; i < argc ; i++)
	{
		if (i + 1 != argc)
		{
			if (std::string(argv[i]) == "-vorace") 
			{
				vorace = true;
            } 
			else if (std::string(argv[i]) == "-dynamique") 
			{
				dynamique = true;
			}
			else if (std::string(argv[i]) == "-branch") 
			{
				BaB = true;
			}
			else if (std::string(argv[i]) == "-config") 
			{
				int nbFile = atoi(argv[++i]);
				for (int j = 0 ; j < nbFile ; j++)
				{
					std::string temp = argv[++i]; 
					extractConfig(temp, echantillons, mapIndex++); 
				}
			}
			else if (std::string(argv[i]) == "-file") 
			{
				int nbFile = atoi(argv[++i]);
				for (int j = 0 ; j < nbFile ; j++)
				{
					std::string temp = argv[++i]; 
					echantillons.insert(std::pair<int, std::string>(mapIndex,temp));
				}
				mapIndex++;
			}
			else
			{

			}
		}
	}

	for (int j = 0 ; j < mapIndex ; j++)
	{
		std::vector<std::string> serie;
		
		std::pair <std::multimap<int,std::string>::iterator, std::multimap<int,std::string>::iterator> ret;
		ret = echantillons.equal_range(j);

		std::multimap<int,std::string>::iterator itMap = ret.first;
		for( itMap ; itMap != ret.second ; itMap++)
		{
			serie.push_back(itMap->second);
		}
		
		Executor exec(serie);
		exec.setVorace(vorace);
		exec.setDynamique(dynamique);
		exec.setBranchAndBound(BaB);
	
		exec.RunAll();

		exec.printCSV();
	}

	printf("TOUT A MARCHE\n");

	return 0;
}

void extractConfig(std::string& file, std::multimap<int, std::string>& map, int key)
{
	// source : http://www.cplusplus.com/doc/tutorial/files/
	std::string line;
	std::ifstream myfile (file.c_str());
	if (myfile.is_open())
	{
		while ( myfile.good() )
		{
			getline (myfile,line);
			map.insert(std::pair<int, std::string>(key,line));
		}
		myfile.close();
	}

}